﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.SalesOrder;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.SalesOrder
{
    public class OrderQuoteServices : IOrderQuoteServices
    {
        public OrderQuoteServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(OrderQuote model, List<OrderQuoteItem> list)
        {
            var result = await Db.InsertIdentityTranAsync(model,list);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete OrderQuote where Id in @Ids;delete OrderQuoteItem where OrderQuoteId in @Ids;";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<IEnumerable<OrderQuote>> GetAllAsync()
        {
            string sql = "select * from OrderQuote";
            var list = await Db.QueryAllAsync<OrderQuote>(sql);
            return list;
        }

        public async Task<IEnumerable<OrderQuote>> GetAsync(int PageIndex, int PageSize, string Number, string CustomerNumber, string EmployeesId, string WarehouseId, string Status)
        {
            string sql = "select * from OrderQuote where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(CustomerNumber))
            {
                sql += " and CustomerNumber like @CustomerNumber ";
            }
            if (!string.IsNullOrEmpty(EmployeesId))
            {
                sql += " and EmployeesId like @EmployeesId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await Db.QueryAsync<OrderQuote>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", CustomerNumber = $"%{CustomerNumber}%", EmployeesId = $"%{EmployeesId}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%" });
            return list;
        }

        public async  Task<int> GetCountAsync(string Number, string CustomerNumber, string EmployeesId, string WarehouseId, string Status)
        {
            string sql = "select count(*) from OrderQuote where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(CustomerNumber))
            {
                sql += " and CustomerNumber like @CustomerNumber ";
            }
            if (!string.IsNullOrEmpty(EmployeesId))
            {
                sql += " and EmployeesId like @EmployeesId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", CustomerNumber = $"%{CustomerNumber}%", EmployeesId = $"%{EmployeesId}%", WarehouseId = $"%{WarehouseId}%" , Status = $"%{Status}%" });
            return count;



        }


        public async Task<IEnumerable<OrderQuoteItemDto>> GetOrderQuotebyIdAsync(string OrderQuoteId, string WareHouseId)
        {
            var list = await Db.QueryAsync<OrderQuoteItemDto>(@"
select  item.Spec,item.ItemName,item.ItemCode,item.UnitOfMeasure,oq.*,(ik.StockCount-ik.SalesCount) as CurrCount from OrderQuoteItem oq
inner join Item item on oq.ItemId = item.Id
inner join ItemStock ik on ik.ItemId=item.Id and ik.WareHouseId=@WareHouseId where oq.OrderQuoteId = @OrderQuoteId", new { OrderQuoteId, WareHouseId });
            return list;
        }

        public async Task<bool> UpdateAsync(OrderQuote model, List<OrderQuoteItem> list)
        {
            var result = await Db.UpdateIdentityTranAsync(model, list);
            return result;
        }

        public async Task<bool> updateQuoteStatus(int Id, string Status)
        {


            string sql = $"update   OrderQuote set Status=@Status where Id=@Id";


            var result = await Db.UpdateAsync(sql,new{Id,Status });
            return result;
        }
    }
}
